Now let’s combine the two maps and show both at the same time.
All we need to do here is pull the AddPolygons() and AddLegend() functions from both individual maps into a single code chunk. No new code at this point. Note that each call has to include the data we are pointing to individually.
leaflet()|>addProviderTiles(providers$CartoDB.Positron)|>setView(lat =34.1, lng =-117.34, zoom =10)|>addPolygons(data =SBDwarehouses, color ='black', weight =1, fillOpacity =0.7)|>addLegend(data =SBDwarehouses, colors ='black', labels ='Existing warehouses')|>addPolygons(data =SBDCoEJ, color =~palHispanic(Hispanic), weight =1)|>addLegend(data =SBDCoEJ, pal =palHispanic, title ="% Hispanic", values =~Hispanic)
Figure 19.3: Warehouses and percent Hispanic
Now let’s add in the layers control to toggle the individual layers on and off.
This requires one new function addLayersControl(). It includes an overlay option called overlayGroups. Lastly we add a new argument in the existing code chunk that defines each layers as a group. Note that including group identifiers for both Polygons and Legends is required to have them both toggle on and off.
Figure 19.4 shows the working toggles as a little stack in the upper right corner above the legends.
leaflet()|>addProviderTiles(providers$CartoDB.Positron)|>setView(lat =34.1, lng =-117.34, zoom =10)|>addLayersControl(overlayGroups =c('Warehouses', '% Hispanic'))|>addPolygons(data =SBDwarehouses, color ='black', weight =1, fillOpacity =0.7, group ='Warehouses')|>addLegend(data =SBDwarehouses, colors ='black', labels ='Existing warehouses', group ='Warehouses')|>addPolygons(data =SBDCoEJ, color =~palHispanic(Hispanic), weight =1, group ='% Hispanic')|>addLegend(data =SBDCoEJ, pal =palHispanic, title ="% Hispanic", values =~Hispanic, group ='% Hispanic')
Figure 19.4: Warehouses and percent Hispanic with overlay toggles
Voila!
19.2.2 Example 2 - Create a buffer around a point or polygon.
The function st_buffer() from the sf package allows one to generate larger shapes surrounding an existing point, line, or polygon.
It only requires the existing dataset and a distance. The only tricky part is figuring out the unit one is buffering in. In some coordinate reference systems, you are in meters. In others, you are in decimal degrees. The buffer distance of 1000 meters is useful, but 1000 latitude degrees goes off map.
Allow me to demonstrate a buffer map with the Gardens dataset. We’ll do a coordinate transformation to NAD83 (a meter based projection) and then back.
gardens<-st_read(dsn ='CommunityGardens.geojson')|>filter(County=='San Bernardino')|>#transform to NAD83 meters based coordinatesst_transform(crs =4269)
Reading layer `CommunityGardens' from data source
`C:\Dev\EA078_Fall2023\CommunityGardens.geojson' using driver `GeoJSON'
Simple feature collection with 186 features and 12 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -119.2673 ymin: 32.78017 xmax: -115.5097 ymax: 34.52903
Geodetic CRS: WGS 84
buffer_gardens<-gardens|>st_buffer(dist =1000)|>#transform back to WGS84 for mapst_transform(crs =4326)leaflet()|>addTiles()|>setView(lat =34.1, lng =-117.34, zoom =10)|>addPolygons(data =SBDCoEJ, color =~palHispanic(Hispanic), weight =1, group ='% Hispanic')|>addPolygons(data =buffer_gardens, color ='darkgreen', fillOpacity =0.8, label =~Name)
Figure 19.5: San Bernardino County census tract 1000 m buffers
19.2.2.1 Class Exercise
Add a layersControl() to this map to toggle on and off the gardens and Hispanic layers.